package cn.edu.zsc.blms;

import cn.edu.zsc.blms.service.IBookService;
import cn.edu.zsc.blms.service.IBorrowingService;
import cn.edu.zsc.blms.service.IReaderService;
import cn.edu.zsc.blms.service.collection.BookService;
import cn.edu.zsc.blms.service.collection.BorrowingService;
import cn.edu.zsc.blms.service.collection.ReaderService;

import java.util.Scanner;


/***
 * @author 杨少靖 2023030103071
 */
public class App {
    public static Scanner input;
    private final IBookService bookService;
    private final IReaderService readerService;
    private final IBorrowingService borrowingService;
    public App(IBookService bookService, IReaderService readerService, IBorrowingService borrowingService){
        this.bookService = bookService;
        this.readerService = readerService;
        this.borrowingService = borrowingService;
    }
    public static void main(String[] args){
        App app = new App(BookService.getSingleton(), ReaderService.getSingleton(), BorrowingService.getSingleton());
        app.run();
    }
    public void readerMenu(){
        System.out.println("------------------");
        System.out.println("    读者管理    ");
        System.out.println("|   0...返回上级    |");
        System.out.println("|   1...添加读者    |");
        System.out.println("|   2...禁用读者    |");
        System.out.println("|   3...编辑读者    |");
        System.out.println("|   4...查询读者    |");
        System.out.println("------------------");
        System.out.println("请输入选项: ");
    }
    public void systemMenu(){
        System.out.println("------------------");
        System.out.println("    图书管理系统    ");
        System.out.println("|   0...退出系统    |");
        System.out.println("|   1...读者管理    |");
        System.out.println("|   2...图书管理    |");
        System.out.println("|   3...借还图书    |");
        System.out.println("------------------");
        System.out.println("请输入选项: ");
    }
    public void bookMenu(){
        System.out.println("------------------");
        System.out.println("    图书管理    ");
        System.out.println("|   0...返回上级    |");
        System.out.println("|   1...添加图书    |");
        System.out.println("|   2...废弃图书    |");
        System.out.println("|   3...编辑图书    |");
        System.out.println("|   4...查询图书    |");
        System.out.println("------------------");
        System.out.println("请输入选项: ");
    }
    public void borrowingMenu(){
        System.out.println("------------------");
        System.out.println("    借还图书    ");
        System.out.println("|   0...返回上级    |");
        System.out.println("|   1...借阅图书    |");
        System.out.println("|   2...归还图书    |");
        System.out.println("|   3...查询记录    |");
        System.out.println("------------------");
        System.out.println("请输入选项: ");
    }
    public void run(){
        input = new Scanner(System.in);
        String systemMenuChoice;
        do{
            systemMenu();
            systemMenuChoice = input.nextLine();
            switch (systemMenuChoice){
                case "0":
                    System.out.println("成功退出系统, 欢迎下次使用");
                    break;
                case "1":
                    // 读者管理
                    String readerMenuChoice;
                    do{
                        readerMenu();
                        readerMenuChoice = input.nextLine();
                        switch (readerMenuChoice) {
                            case "0":
                                break;
                            case "1":
                                //添加读者
                                readerService.create();
                                break;
                            case "2":
                                //禁用读者
                                readerService.disable();
                                break;
                            case "3":
                                //编辑读者
                                readerService.update();
                                break;
                            case "4":
                                //查询读者
                                readerService.query();
                                break;
                            default:
                                System.out.println("输入非法, 请输入正确的菜单编号! ");
                        }
                    }while(!"0".equals(readerMenuChoice));
                    break;
                case "2":
                    // 图书管理
                    String bookMenuChoice;
                    do{
                        bookMenu();
                        bookMenuChoice = input.nextLine();
                        switch (bookMenuChoice) {
                            case "0":
                                break;
                            case "1":
                                //添加图书
                                bookService.create();
                                break;
                            case "2":
                                //废弃图书
                                bookService.disable();
                                break;
                            case "3":
                                //编辑图书
                                bookService.update();
                                break;
                            case "4":
                                //TODO:查询图书
                                bookService.query();
                                break;
                            default:
                                System.out.println("输入非法, 请输入正确的菜单编号! ");
                        }
                    }while(!"0".equals(bookMenuChoice));
                    break;
                case "3":
                    //  TODO:借还管理
                    String borrowingMenuChoice;
                    do{
                        borrowingMenu();
                        borrowingMenuChoice = input.nextLine();
                        switch (borrowingMenuChoice) {
                            case "0":
                                break;
                            case "1":
                                //借阅图书
                                borrowingService.borrow();
                                break;
                            case "2":
                                //归还图书
                                borrowingService.giveBack();
                                break;
                            case "3":
                                //查询记录
                                borrowingService.query();
                                break;
                            default:
                                System.out.println("输入非法, 请输入正确的菜单编号! ");
                        }
                    }while(!"0".equals(borrowingMenuChoice));
                    break;
                default:
                    System.out.println("输入非法, 请输入正确的菜单编号! ");
            }
        }while(!"0".equals(systemMenuChoice));
        input.close();
    }
}
